<?php

namespace app\models;

use app\admin\service\CommonService;
use think\Model;

class StudentModel extends Model
{
	protected $table = 'edu_student';

	/**
	 * 获取学员列表.
	 *
	 * @param $param
	 *
	 * @throws \think\Exception
	 * @throws \think\exception\DbException
	 *
	 * @return array
	 */
	public function getStudentLists($param)
	{
		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
		$where = [];

		if (isset($param['keywords']) && '' !== $param['keywords']) {
			$where['phone|customer_name|guardian_phone|other_phone|name'] = ['like', '%'.$param['keywords'].'%'];
		}
		if (isset($param['day']) && '' !== $param['day']) {
			$date = explode(' - ', $param['day']);
			$start = $date[0];
			$end = $date[1];
			$where['next_follow_up_date'] = [['egt', $start], ['elt', $end]];
		}
		if (isset($param['user_id']) && '' !== $param['user_id']) {
			$where['user_id'] = $param['user_id'];
			$where['status'] = ['neq',2];
		}

		if (isset($param['intentional_grade']) && '' !== $param['intentional_grade']) {
			$where['intentional_grade'] = $param['intentional_grade'];
		}

		if (isset($param['status']) && '' !== $param['status']) {
			$where['status']  = $param['status'];
		}

		if (isset($param['type']) && '' !== $param['type']) {
			$type = $param['type'];
		} else {
			$type = 'all';
		}

		switch ($type) {
			case 'today':
				//今天安排客户
				$where['next_follow_up_date'] = date('Y-m-d');
				$where['status'] = 1;
				break;
			case 'today_add':
				//今天新增客户
				$where['register_date'] = date('Y-m-d');
				$where['status'] = 1;
				break;
			case 'today_follow_up':
				//今天跟进客户
				$where['follow_up_date'] = date('Y-m-d');
				$where['status'] = 1;
				break;
			case 'today_switch_to':
				//今天转入客户
				$where['status'] = 1;
				$where['switch_to'] = 1;
				break;
			case 'my':
				//我的客户
				$where['user_id'] = get_current_id();
				$where['status'] = 1;
				break;
			case 'public':
				//公共客户
				$where['user_id'] = 0;
				$where['status'] = 1;
				break;
			case 'invalid':
				//无效客户
				$where['user_id'] = 0;
				$where['status'] = 2;
				break;
			case 'assigned':
				//已分配客户
				$where['user_id'] = ['not in',get_current_id()];
				$where['course_consultant_type'] = 1;
				$where['status'] = 1;
				break;
			case 'legacy':
				//遗留客户
				$where['next_follow_up_timestamp'] = ['lt',time()];
				$where['status'] = 1;
				break;
			case 'nocontact':
				//未联系客户
				$where['intentional_grade'] = 0;
				$where['status'] = 1;
				break;
			case 'all':
				break;
		}

		$listData = self::where($where)->order('next_follow_up_date asc')
			->paginate($listRows, false, ['page' => $page])
			->toArray();

		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {
				if (0 === $item['intentional_grade']) {
					$item['intentional_grade_text'] = '0类';
				} elseif (1 === $item['intentional_grade']) {
					$item['intentional_grade_text'] = '1类';
				} elseif (2 === $item['intentional_grade']) {
					$item['intentional_grade_text'] = '2类';
				} elseif (3 === $item['intentional_grade']) {
					$item['intentional_grade_text'] = '3类';
				} elseif (4 === $item['intentional_grade']) {
					$item['intentional_grade_text'] = '4类';
				} elseif (5 === $item['intentional_grade']) {
					$item['intentional_grade_text'] = '5类';
				}

				/***待分配**/
				if($type == 'assigned' && get_current_type() == 5){
					$item['phone'] = substr($item['phone'],0,3).'****'.substr($item['phone'],7,4);
				}
			}
		}

		return $listData;
	}

	public function getAppointmentManageStudentLists($param)
	{
		$GradesDetailsModel = new GradesDetailsModel();  // 班级
		$StudentSubjectClassModel = new StudentSubjectClassModel();   // 学生

		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
		$where = [];
		//关键词
		if (isset($param['keywords']) && '' !== $param['keywords']) {
			if(isset($param['stype'])){
				if ($param['stype'] == ''){
					$where['student_name|name'] = ['like', '%'.$param['keywords'].'%'];
				}elseif ($param['stype'] == '2'){
					$where['phone'] = $param['keywords'];
				}elseif ($param['stype'] == '3'){
					$where['father_name|mother_name'] = ['like', '%'.$param['keywords'].'%'];
				}elseif ($param['stype'] == '4'){
					$where['father_phone|mother_phone'] = $param['keywords'];
				}
			}
		}
		//选择校区
		if (isset($param['select_campus']) && '' !== $param['select_campus']) {
			$where['campus_id'] = $param['select_campus'];
		}
		//上门状态
		if (isset($param['door_state']) && '' !== $param['door_state']) {
			if($param['door_state'] == 1){  //已预约
				$where['follow_up'] = 36;
			}else if($param['door_state'] == 2){  //已上门
				$where['follow_up'] = 103;
			} else if($param['door_state'] == 3){  //过期未上门
				$where['follow_up'] = -1;
			}
		}
		//课程顾问
		if (isset($param['user_id']) && '' !== $param['user_id']) {
			$where['user_id'] = $param['user_id'];
		}
		//日期
		if (isset($param['day']) && '' !== $param['day']) {
			$date = explode(' - ', $param['day']);
			$start = $date[0];
			$end = $date[1];
			$where['register_date'] = [['egt', $start], ['elt', $end]];
		}
		//来源
		if (isset($param['channel']) && '' !== $param['channel']) {
			$where['channel'] = $param['channel'];
		}
		//获取学员列表[通过班级找学生]
		if(get_current_type() == 5){
			if(get_current_group_type() == 2){  //老师类
				$grades_res = $GradesDetailsModel->where(['user_teacher_id' => get_current_id()])->column('id');
				$grades_res_str = implode(',',$grades_res);
				$student_res = $StudentSubjectClassModel->where(['class_subject_id' => ['in',$grades_res_str]])->column('student_id');
				$student_res = array_unique($student_res);
				$student_res_str = implode(',',$student_res);
				$where['id'] = ['in',$student_res_str];
			}else if(get_current_group_type() == 3){  //助教类
				$grades_res = $GradesDetailsModel->where(['user_assistant_id' => get_current_id()])->column('id');
				$grades_res_str = implode(',',$grades_res);
				$student_res = $StudentSubjectClassModel->where(['class_subject_id' => ['in',$grades_res_str]])->column('student_id');
				$student_res = array_unique($student_res);
				$student_res_str = implode(',',$student_res);
				$where['id'] = ['in',$student_res_str];
			}else if(get_current_group_type() == 4){ //班主任
				$grades_res = $GradesDetailsModel->where(['user_master_id' => get_current_id()])->column('id');
				$grades_res_str = implode(',',$grades_res);
				$student_res = $StudentSubjectClassModel->where(['class_subject_id' => ['in',$grades_res_str]])->column('student_id');
				$student_res = array_unique($student_res);
				$student_res_str = implode(',',$student_res);
				$where['id'] = ['in',$student_res_str];
			}else if(get_current_group_type() == 5){ //教务类
				$where['user_edu_id'] = array(array('like','%'.get_current_id().'%'), array('eq',0), array('eq',null), 'or');
			}
		}

		//已上门，已预约
		$where['follow_up']  = ['in','103,36,-1'];

		//0试听 1客户
		$where['is_audition']  = ['in','0,1'];

		$where['status'] = 1;
		$listData = self::where($where)->order('update_time desc')
			->paginate($listRows, false, ['page' => $page])
			->toArray();

		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {
				$channel = DictionaryModel::get($item['channel'])['name'];
				$item['channel'] = $channel ? $channel : '-';
				$item['campus_name'] = CampusModel::get(['id' => $item['campus_id']])['campus_name'];
				$item['follow_up_text'] =DictionaryModel::get($item['follow_up'])['name'];
				if ($item['follow_up'] == -1) {  //已预约
					$item['follow_up_text'] = '<span style="color:#FF5722;font-weight: bold;">过期未上门</span>';
				}
				if (1 === $item['intentional_grade']) {
					$item['intentional_grade_text'] = '<i class="layui-icon" style="color: yellow">&#xe67a;</i>';
				} elseif (2 === $item['intentional_grade']) {
					$item['intentional_grade_text'] = '<i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>';
				} elseif (3 === $item['intentional_grade']) {
					$item['intentional_grade_text'] = '<i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>';
				} elseif (4 === $item['intentional_grade']) {
					$item['intentional_grade_text'] = '<i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>';
				} elseif (5 === $item['intentional_grade']) {
					$item['intentional_grade_text'] = '<i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>
                                                        <i class="layui-icon" style="color: yellow">&#xe67a;</i>';
				}
				/*********上门时间(预约时间)***********/
				$item['visiting_date'] = $item['visiting_date'] ? $item['visiting_date'] : '--';
				/***********到访时间***************/
				if($item['visiting_datetime'] > 0){
					$item['visiting_datetime'] = date('Y-m-d H:i:s',$item['visiting_datetime']);
				}else{
					$item['visiting_datetime'] = '--';
				}
			}
		}

		return $listData;
	}

	/***获取添加学员列表**/
	public function getAddStudentsLists($param){

		$GradesDetailsModel = new GradesDetailsModel();  // 班级
		$StudentSubjectClassModel = new StudentSubjectClassModel();   // 学生

		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
		$where = ['status' => 1, 'is_audition' => array('neq',1)];
		/*
		$where = ['is_audition' => 0 , 'status' => 1];
		$where = ['sign_up' => 1 , 'status' => 1];
		*/
		if (isset($param['param']) && '' !== $param['param']) {
			$start_str = substr($param['param'],0,1);
			if($start_str == 'X'){
				$param['student_number'] = $param['param'];  //学号
			}else if(is_numeric($param['param'])){
				$param['phone'] = $param['param'];  //电话
			}else{
				$param['name'] = $param['param'];  //姓名
			}
		}
		if (isset($param['name']) && '' !== $param['name']) {
			$where['name'] = ['like', $param['name'].'%'];
		}
		if (isset($param['phone']) && '' !== $param['phone']) {
			$where['phone'] = ['like', $param['phone'].'%'];
		}
		if (isset($param['campus_id']) && '' !== $param['campus_id']) {
			$where['campus_id'] = $param['campus_id'];
		}


		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}

		//获取学员列表[通过班级找学生]
		if(get_current_type() == 5){
			if(get_current_group_type() == 2){  //老师类
				$grades_res = $GradesDetailsModel->where(['user_teacher_id' => get_current_id()])->column('id');
				$grades_res_str = implode(',',$grades_res);
				$student_res = $StudentSubjectClassModel->where(['class_subject_id' => ['in',$grades_res_str]])->column('student_id');
				$student_res = array_unique($student_res);
				$student_res_str = implode(',',$student_res);
				$where['id'] = ['in',$student_res_str];
			}else if(get_current_group_type() == 3){  //助教类
				$grades_res = $GradesDetailsModel->where(['user_assistant_id' => get_current_id()])->column('id');
				$grades_res_str = implode(',',$grades_res);
				$student_res = $StudentSubjectClassModel->where(['class_subject_id' => ['in',$grades_res_str]])->column('student_id');
				$student_res = array_unique($student_res);
				$student_res_str = implode(',',$student_res);
				$where['id'] = ['in',$student_res_str];
			}else if(get_current_group_type() == 4){ //班主任
				$grades_res = $GradesDetailsModel->where(['user_master_id' => get_current_id()])->column('id');
				$grades_res_str = implode(',',$grades_res);
				$student_res = $StudentSubjectClassModel->where(['class_subject_id' => ['in',$grades_res_str]])->column('student_id');
				$student_res = array_unique($student_res);
				$student_res_str = implode(',',$student_res);
				$where['id'] = ['in',$student_res_str];
			}else if(get_current_group_type() == 5){ //教务类
				$where['user_edu_id'] = array(array('like','%'.get_current_id().'%'), array('eq',0), array('eq',null), 'or');
			}
		}


		$listData = self::where($where)->order('update_time desc')->paginate($listRows, false, ['page' => $page])
			->toArray();

		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {

				if($item['sex'] == 1){
					$item['sex'] = '男';
				}else if($item['sex'] == 2){
					$item['sex'] = '女';
				}

				if($item['sign_up'] != 1 && $item['is_audition'] == 0){
					$item['contract_number'] = '-';
					$item['student_type'] = '试听学员';
				}else if($item['sign_up'] == 1 && $item['is_audition'] == 2){
					$item['student_type'] = '学员正式';
				}

				if($item['intentional_course'] == null || strlen($item['intentional_course']) <= 0){
					$item['intentional_course'] = '-';
				}

				if($item['grade'] != 0 &&  $item['grade'] != null){
					$item['grade'] = DictionaryModel::get(['id' => $item['grade']])['name'];
				}else{
					$item['grade'] = '-';
				}

				if($item['campus_id'] != 0 && $item['campus_id'] != null){
					$item['campus_name'] = CampusModel::get(['id' => $item['campus_id']])['campus_name'];
				}else{
					$item['campus_name'] = '-';
				}

			}
		}
		return $listData;
	}



	/**
	 * 就读班级
	 * modify by Small Y
	 *
	 * @param $param
	 * @return array
	 * @throws \think\exception\DbException
	 */
	public function getClass($param)
	{
		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
		$where = [];
		$where_ = [];

		$student_id = $param['id'];  //学生ID

		if (isset($param['grades_name']) && '' !== $param['grades_name']) {
			$where_['grades_name'] = ['like', '%'.$param['grades_name'].'%'];;
		}

		$where['student_id'] = $student_id;
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
			$where_['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
			$where_['campus_id'] = ['in',get_current_campus_id()];
		}
		$StudentSubjectClassModel = new StudentSubjectClassModel();
		$GradesDetailsModel = new GradesDetailsModel();
		$ContractModel = new ContractModel();
		$grades_res = $StudentSubjectClassModel->where($where)->column('class_subject_id');
		$grades_res = array_unique($grades_res);
		$grades_str = implode(',',$grades_res);


		$where_['graduation_status'] = $param['graduation_status'];//未结业还在就读
		$where_['id'] = ['in',$grades_str];

		$listData = $GradesDetailsModel->where($where_)->paginate($listRows, false, ['page' => $page])
			->toArray();

		if (!empty($listData)) {
			foreach ($listData['data'] as &$item){
				$type = CourseModel::get(['id' => $item['course_id']])['course_type'];
				$item['course_type'] = DictionaryModel::get(['id'=>$type])['name'];
				$res = $ContractModel->where(['student_id' => $student_id])->order('signing_date asc')->find();
				$item['sign_up_date'] = $res['signing_date'];
				$item['already_grades_class'] = $item['already_arranged'] .'/'. ($item['already_class'] - $item['already_arranged']);
				$item['total'] = $ContractModel->where(['student_id' => $student_id])->sum('balance') / 100;

			}
		}

		return $listData;
	}

	/**
	 * 跟进状态
	 *
	 * @param $follow_up
	 *
	 * @return mixed
	 */
	public function getFollowUpText($follow_up)
	{
		$value = '';
		switch ($follow_up) {
			case -1:
				$value = '未处理';

				break;
			case -2:
				$value = '已丢单';

				break;
			case 1:
				$value = '已联系';

				break;
			case 2:
				$value = '预约试听';

				break;
			case 3:
				$value = '邀约上门';

				break;
			case 4:
				$value = '已交订金';

				break;
		}

		return $value;
	}

	/****统计跟进状态***/
	public function getFollowStatisticsList($param){
		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
		$where = ['status' => 1];
		if (isset($param['campus_name']) && '' !== $param['campus_name']) {
			$where['campus_name'] = ['like', $param['campus_name'].'%'];
		}

		if (isset($param['campus_id']) && '' !== $param['campus_id']) {
			$where['campus_id'] = ['like', $param['campus_id'].'%'];
		}
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}

		if(get_current_type() == 5){  //员工
			$where['user_id'] = get_current_id();
		}

		$untreated = 0;
		$lost_order = 0;
		$contacted = 0;
		$appointment_auditioning = 0;
		$invite_home = 0;
		$deposit_paid = 0;
		$unallocated = 0;
		$listData = self::where($where)
			->paginate($listRows, false, ['page' => $page])
			->toArray();
		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {
				//34:未处理，105:已丢单， 35:已跟进，36:已预约，103:已上门，104:已交定金，33:未分配
				if($item['follow_up'] == 34){
					$item['untreated'] = 1;
					$item['lost_order'] = '---';
					$item['contacted'] = '---';
					$item['appointment_auditioning']  = '---';
					$item['invite_home'] = '---';
					$item['deposit_paid'] = '---';
					$item['unallocated'] = '---';
					$untreated = $untreated + 1;
				}else if($item['follow_up'] == 105){
					$item['untreated'] = '---';
					$item['lost_order'] = 1;
					$item['contacted'] = '---';
					$item['appointment_auditioning']  = '---';
					$item['invite_home'] = '---';
					$item['deposit_paid'] = '---';
					$item['unallocated'] = '---';
					$lost_order = $lost_order + 1;
				}else if($item['follow_up'] == 35){
					$item['untreated'] = '---';
					$item['lost_order'] = '---';
					$item['contacted'] = 1;
					$item['appointment_auditioning']  = '---';
					$item['invite_home'] = '---';
					$item['deposit_paid'] = '---';
					$item['unallocated'] = '---';
					$contacted = $contacted + 1;
				}else if($item['follow_up'] == 36){
					$item['untreated'] = '---';
					$item['lost_order'] = '---';
					$item['contacted'] = '---';
					$item['appointment_auditioning']  = 1;
					$item['invite_home'] = '---';
					$item['deposit_paid'] = '---';
					$item['unallocated'] = '---';
					$appointment_auditioning = $appointment_auditioning + 1;
				}else if($item['follow_up'] == 103){
					$item['untreated'] = '---';
					$item['lost_order'] = '---';
					$item['contacted'] = '---';
					$item['appointment_auditioning']  = '---';
					$item['invite_home'] = 1;
					$item['deposit_paid'] = '---';
					$item['unallocated'] = '---';
					$invite_home = $invite_home + 1;
				}else if($item['follow_up'] == 104){
					$item['untreated'] = '---';
					$item['lost_order'] = '---';
					$item['contacted'] = '---';
					$item['appointment_auditioning']  = '---';
					$item['invite_home'] = '---';
					$item['deposit_paid'] = 1;
					$item['unallocated'] = '---';
					$deposit_paid = $deposit_paid + 1;
				}else if($item['follow_up'] == 33){
					$item['untreated'] = '---';
					$item['lost_order'] = '---';
					$item['contacted'] = '---';
					$item['appointment_auditioning']  = '---';
					$item['invite_home'] = '---';
					$item['deposit_paid'] = '---';
					$item['unallocated'] = 1;
					$unallocated = $unallocated + 1;
				}
			}
		}

		$item_end['name'] = '线索';
		$item_end['untreated'] = $untreated;
		$item_end['lost_order'] = $lost_order;
		$item_end['contacted'] = $contacted;
		$item_end['appointment_auditioning']  = $appointment_auditioning;
		$item_end['invite_home'] = $invite_home;
		$item_end['deposit_paid'] = $deposit_paid;
		$item_end['unallocated'] = $unallocated;
		$listData['data'][count($listData['data'])] = $item_end;

		return $listData;

	}

	/***
	 *获取总校区意向级别
	 *
	 **/
	public function getLevelStatisticsList($param)
	{
		$where = ['status' => 1];
		$listData['data'] = [];

		//时间段
		if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
			$where['update_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
		}

		$count_student = self::where($where)->count();
		$level_count = 0;
		//意向级别
		for($i = 0; $i <= 5; $i ++ ){
			$where['intentional_grade'] = $i;
			$item = [];
			$item['name'] = $i.'类';
			$item['count'] = self::where($where)->count();
			if($count_student <= 0){
				$item['rate'] = '0%';
			}else{
				$item['rate'] = sprintf("%.2f",(($item['count'] / $count_student) * 100)).'%';
			}
			$level_count = $level_count +  $item['count'];
			$listData['data'][] = $item;
		}

		$item_end['name'] = '合计';
		$item_end['count'] = $count_student;
		if($count_student <= 0){
			$item_end['rate'] = '0%';
		}else{
			$item_end['rate'] = sprintf("%.2f",(($level_count / $count_student) * 100)).'%';
		}
		$listData['data'][count($listData['data'])] = $item_end;

		return $listData;

	}

	/***
	 *获取单个校区意向级别
	 *
	 **/
	public function getLevelOnlyStatisticsList($param){
		$campus_id = $param['campus_id'];
		$where = ['status' => 1, 'campus_id' => $campus_id];

		if(get_current_type() == 5){  //员工级别
			$where['user_id'] = get_current_id();
		}

		$listData['data'] = [];

		//时间段
		if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
			$where['update_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
		}

		$count_student = self::where($where)->count();
		$level_count = 0;
		//意向级别
		for($i = 0; $i <= 5; $i ++ ){
			$where['intentional_grade'] = $i;
			$item = [];
			$item['name'] = $i.'颗星';
			$item['count'] = self::where($where)->count();
			if($count_student <= 0){
				$item['rate'] = '0%';
			}else{
				$item['rate'] = sprintf("%.2f",(($item['count'] / $count_student) * 100)).'%';
			}
			$level_count = $level_count +  $item['count'];
			$listData['data'][] = $item;
		}


		$item_end['name'] = '合计';
		$item_end['count'] = $count_student;
		if($count_student <= 0){
			$item_end['rate'] = '0%';
		}else{
			$item_end['rate'] = sprintf("%.2f",(($level_count / $count_student) * 100)).'%';
		}
		$listData['data'][count($listData['data'])] = $item_end;

		return $listData;

	}

	//获取学员列表
	public function getStudentSingUpLists($param){

		$StudentSubjectInfosClassModel = new StudentSubjectInfosClassModel();  //详情
		$ContractModel = new ContractModel();   //订单
		$GradesDetailsModel = new GradesDetailsModel();  // 班级
		$StudentSubjectClassModel = new StudentSubjectClassModel();   // 学生
		$CourseModel = new CourseModel(); //课程

		$student_id_one = [];
		$student_id_two = [];
		$student_id_three = [];
		$student_id_four = [];

		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
		$where = ['sign_up' => 1, 'status' => 1];
		if (isset($param['keywords']) && '' !== $param['keywords']) {
			if(isset($param['stype'])){
				if ($param['stype'] == ''){
					$where['student_name|name'] = ['like', $param['keywords'].'%'];
				}elseif ($param['stype'] == '2'){
					$where['phone'] = $param['keywords'];
				}elseif ($param['stype'] == '3'){
					$where['father_name|mother_name'] = ['like', $param['keywords'].'%'];
				}elseif ($param['stype'] == '4'){
					$where['father_phone|mother_phone'] = $param['keywords'];
				}
			}
		}

		//时间段
		if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
			if(isset($param['status_type'])){
				if ($param['status_type'] == ''){
					$where['update_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59')); //最后操作时间
				}elseif ($param['status_type'] == '2'){
					$where['next_follow_up_record'] = array('between', array($param['start_date'],$param['end_date'])); //下次待跟进时间
				}elseif ($param['status_type'] == '3'){
					$where['create_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59')); //创建时间
				}elseif ($param['status_type'] == '4'){
					$where['birth'] = array('between', array($param['start_date'],$param['end_date'])); //生日
				}elseif ($param['status_type'] == '5'){
					$where['register_date'] = array('between', array($param['start_date'],$param['end_date'])); //登记日期
				}
			}
		}

		if (isset($param['day']) && '' !== $param['day']) {
			$date = explode(' - ', $param['day']);
			$start = $date[0];
			$end = $date[1];
			$where['register_date'] = [['egt', $start], ['elt', $end]];
		}
		if (isset($param['user_id']) && '' !== $param['user_id']) {
			$where['user_id'] = $param['user_id'];
			$where['status'] = ['neq',2];
		}
		if (isset($param['channel']) && '' !== $param['channel']) {
			$where['channel'] = $param['channel'];
		}


		if (isset($param['follow_up']) && '' !== $param['follow_up']) {
			$where['follow_up'] = $param['follow_up'];
		}

		if (isset($param['personal_center_status']) && '' !== $param['personal_center_status']) {
			$where['personal_center_status'] = $param['personal_center_status'];
		}

		//获取学员列表[通过班级找学生]
		if(get_current_type() == 5){
			if(get_current_group_type() == 2){  //老师类
				$grades_res = $GradesDetailsModel->where(['user_teacher_id' => get_current_id()])->column('id');
				$grades_res_str = implode(',',$grades_res);
				$student_res = $StudentSubjectClassModel->where(['class_subject_id' => ['in',$grades_res_str]])->column('student_id');
				$student_res = array_unique($student_res);
				$student_id_one = $student_res;
				$student_res_str = implode(',',$student_res);
				$where['id'] = ['in',$student_res_str];
			}else if(get_current_group_type() == 3){  //助教类
				$grades_res = $GradesDetailsModel->where(['user_assistant_id' => get_current_id()])->column('id');
				$grades_res_str = implode(',',$grades_res);
				$student_res = $StudentSubjectClassModel->where(['class_subject_id' => ['in',$grades_res_str]])->column('student_id');
				$student_res = array_unique($student_res);
				$student_id_one = $student_res;
				$student_res_str = implode(',',$student_res);
				$where['id'] = ['in',$student_res_str];
			}else if(get_current_group_type() == 4){ //班主任
				$grades_res = $GradesDetailsModel->where(['user_master_id' => get_current_id()])->column('id');
				$grades_res_str = implode(',',$grades_res);
				$student_res = $StudentSubjectClassModel->where(['class_subject_id' => ['in',$grades_res_str]])->column('student_id');
				$student_res = array_unique($student_res);
				$student_id_one = $student_res;
				$student_res_str = implode(',',$student_res);
				$where['id'] = ['in',$student_res_str];
			}else if(get_current_group_type() == 5){ //教务类
				$where['user_edu_id'] =  array(array('like','%'.get_current_id().'%'), array('eq',0), array('eq',null), 'or');
			}
		}

		if (isset($param['grade_class']) && '' !== $param['grade_class']) {   //班级
			$student_res = $StudentSubjectClassModel->where(['class_subject_id' => $param['grade_class']])->column('student_id');
			$student_res = array_unique($student_res);
			$student_id_two = $student_res;
			$student_res_str = implode(',',$student_res);
			$where['id'] = ['in',$student_res_str];
		}

		if (isset($param['grade_headmaster']) && '' !== $param['grade_headmaster']) {   //班主任
			$grades_res = $GradesDetailsModel->where(['user_master_id' => $param['grade_headmaster']])->column('id');
			$grades_res_str = implode(',',$grades_res);
			$student_res = $StudentSubjectClassModel->where(['class_subject_id' => ['in',$grades_res_str]])->column('student_id');
			$student_res = array_unique($student_res);
			$student_id_three = $student_res;
			$student_res_str = implode(',',$student_res);
			$where['id'] = ['in',$student_res_str];
		}

		if (isset($param['type']) && '' !== $param['type']) {  //未排课
			if($param['type'] == 'no_all'){
				$param['timetable'] = 0;
			}
		}


		if (isset($param['timetable']) && '' !== $param['timetable']) {   //是否排课
			$res = self::select();
			$student_arr = [];
			foreach ($res as $key => $value){
				//是否排课
				$timetable = 1;
				if($value['contract_number'] != null){
					$contract_arr = explode(',',$value['contract_number']);
					for($i = 0; $i < count($contract_arr); $i ++){
						$contract_course = $ContractModel->get(['contract_number' => $contract_arr[$i]])['course'];
						$contract_course = explode(',',$contract_course);
						for($a =0 ; $a < count($contract_course); $a ++){
							$student_infos = $StudentSubjectInfosClassModel->get(['student_id' => $value['id'],'course_id' => $contract_course[$a], 'campus_id' => get_current_campus_id()]);
							if(!$student_infos){
								$timetable = 0;
								break;
							}
						}

					}
				}

				if($param['timetable'] == 1){  //已排
					if($timetable == 1){
						$student_arr[] = $value['id'];
					}
				}

				if($param['timetable'] == 0){  //未排
					if($timetable == 0){
						$student_arr[] = $value['id'];
					}
				}
			}
			$student_id_four = $student_arr;
			$student_str = implode(',',$student_arr);
			$where['id'] = array('in',$student_str);
		}

		if (isset($param['select_campus']) && '' !== $param['select_campus']) {   //选择校区
			$where['campus_id'] = $param['select_campus'];
		}else {
			if (get_current_type() == 4 || get_current_type() == 5) {
				$where['campus_id'] = get_current_campus_id();
			} elseif (get_current_type() == 2 || get_current_type() == 3) {
				$where['campus_id'] = ['in', get_current_campus_id()];
			}
		}

		if (isset($param['student_name_select']) && '' !== $param['student_name_select']) {
			$where['student_name'] = ['like', '%'.$param['student_name_select'].'%'];
		}



		if(get_current_type() == 5) {
			if (get_current_group_type() == 2 || get_current_group_type() == 3 || get_current_group_type() == 4) {
				$parameters = [];
				$parameters[] = $student_id_one;
				if(isset($param['grade_class']) && '' !== $param['grade_class']){
					$parameters[] = $student_id_two;
				}
				if(isset($param['grade_headmaster']) && '' !== $param['grade_headmaster']){
					$parameters[] = $student_id_three;
				}
				if(isset($param['timetable']) && '' !== $param['timetable']){
					$parameters[] = $student_id_four;
				}

				$count = count($parameters);
				if($count >= 2){
					$res = '';
					if($count == 2){
						$res = array_intersect($parameters[0],$parameters[1]);
					}else if($count == 3){
						$res = array_intersect($parameters[0],$parameters[1],$parameters[2]);
					}else if($count == 4){
						$res = array_intersect($parameters[0],$parameters[1],$parameters[2],$parameters[3]);
					}
					$res_str = implode(',',$res);
					$where['id'] = ['in',$res_str];

				}




			}
		}



		//1v1
		$course_id_1v1_arr = $CourseModel->where(['course_type' => 41])->column('id');
		$grades_id_1v1_arr = $GradesDetailsModel->where(['course_id' => ['in', implode(',',$course_id_1v1_arr)]])->column('id');
		$grades_id_1v1_arr_str = implode(',',$grades_id_1v1_arr);
		//1vN
		$course_id_1vn_arr = $CourseModel->where(['course_type' => 43])->column('id');
		$grades_id_1vn_arr = $GradesDetailsModel->where(['course_id' => ['in', implode(',',$course_id_1vn_arr)]])->column('id');
		$grades_id_1vn_arr_str = implode(',',$grades_id_1vn_arr);
		//班课
		$course_id_class_arr = $CourseModel->where(['course_type' => 42])->column('id');
		$grades_id_class_arr = $GradesDetailsModel->where(['course_id' => ['in', implode(',',$course_id_class_arr)]])->column('id');
		$grades_id_class_arr_str = implode(',',$grades_id_class_arr);


		$where_elimination_1v1 = [];
		$where_elimination_1v1['status'] = 1;
		$where_elimination_1v1['give_status'] = 1;
		$where_elimination_1v1['class_subject_id'] = ['in', $grades_id_1v1_arr_str];

		$where_elimination_1v1_arrears = [];
		$where_elimination_1v1_arrears['status'] = 1;
		$where_elimination_1v1_arrears['give_status'] = 1;
		$where_elimination_1v1_arrears['class_subject_id'] = ['in', $grades_id_1v1_arr_str];
		$where_elimination_1v1_arrears['arrears_status'] = 1;


		$where_elimination_1vn = [];
		$where_elimination_1vn['status'] = 1;
		$where_elimination_1vn['give_status'] = 1;
		$where_elimination_1vn['class_subject_id'] = ['in', $grades_id_1vn_arr_str];

		$where_elimination_1vn_arrears = [];
		$where_elimination_1vn_arrears['status'] = 1;
		$where_elimination_1vn_arrears['give_status'] = 1;
		$where_elimination_1vn_arrears['class_subject_id'] = ['in', $grades_id_1vn_arr_str];
		$where_elimination_1vn_arrears['arrears_status'] = 1;


		$where_elimination_class = [];
		$where_elimination_class['status'] = 1;
		$where_elimination_class['give_status'] = 1;
		$where_elimination_class['class_subject_id'] = ['in', $grades_id_class_arr_str];

		$where_elimination_class_arrears = [];
		$where_elimination_class_arrears['status'] = 1;
		$where_elimination_class_arrears['give_status'] = 1;
		$where_elimination_class_arrears['class_subject_id'] = ['in', $grades_id_class_arr_str];
		$where_elimination_class_arrears['arrears_status'] = 1;

		//时间段
		if (isset($param['start_date_elimination']) && '' !== $param['start_date_elimination'] && isset($param['end_date_elimination']) && '' !== $param['end_date_elimination']) {
			$where_elimination_1v1['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
			$where_elimination_1v1_arrears['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
			$where_elimination_1vn['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
			$where_elimination_1vn_arrears['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
			$where_elimination_class['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
			$where_elimination_class_arrears['class_date'] = array('between', array($param['start_date_elimination'],$param['end_date_elimination']));
		}


		$listData = self::where($where)->order('update_time desc')
			->paginate($listRows, false, ['page' => $page])
			->toArray();

		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {

				$item['balance'] = ($ContractModel->where(['student_id' => $item['id']])->sum('balance')/100);

				$item['storedvalue'] =  ($item['storedvalue'] / 100);

				if($item['personal_center_status'] == 1){
					$item['personal_center_status'] = '<span style="color:#01AAED;font-weight: bold;">已绑定</span>';
				}else{
					$item['personal_center_status'] =  '<span style="color:#FF5722;font-weight: bold;">未绑定</span>';
				}

				if($item['sex'] === 1){
					$item['sex'] = '男';
				}else if($item['sex'] === 2){
					$item['sex'] = '女';
				}

				$item['follow_up_text'] =DictionaryModel::get($item['follow_up'])['name'];

				$channel = DictionaryModel::get($item['channel'])['name'];
				$item['channel'] = $channel ? $channel : '-';

				$res_student = $StudentSubjectClassModel->where(['student_id' => $item['id'], 'student_type' => array('neq',4)])->select();
				$grades_arr = [];
				$headmaster_arr = [];
				$item['headmaster'] = '-';  //班主任
				foreach ($res_student as $key => $value){
					if(!in_array($value['class_subject_id'], $grades_arr)){   //不存在班课ID
						$grades_arr[] = $value['class_subject_id']; //放入数组
						$res_grades = $GradesDetailsModel->get(['id' => $value['class_subject_id']]);
						if(!in_array($res_grades['headmaster'], $headmaster_arr)){
							$headmaster_arr[] = $res_grades['headmaster'];
							$item['headmaster'] = $item['headmaster'].$res_grades['headmaster'].'-';
						}
					}

				}


				//是否排课
				$timetable = 1;
				if($item['contract_number'] != null){
					$contract_arr = explode(',',$item['contract_number']);
					for($i = 0; $i < count($contract_arr); $i ++){
						$contract_course = $ContractModel->get(['contract_number' => $contract_arr[$i]])['course'];
						$contract_course = explode(',',$contract_course);
						for($a =0 ; $a < count($contract_course); $a ++){
							$student_infos = $StudentSubjectInfosClassModel->get(['student_id' => $item['id'],'course_id' => $contract_course[$a], 'campus_id' => get_current_campus_id()]);
							if(!$student_infos){
								$timetable = 0;
								break;
							}
						}

					}
				}

				if($timetable){
					$item['timetable'] = '<span style="color:#01AAED;font-weight: bold;">是</span>';
				}else{
					$item['timetable'] = '<span style="color:#FF5722;font-weight: bold;">否</span>';
				}



				$class_num = $StudentSubjectInfosClassModel->where(['student_id' => $item['id']])->count(); //已排课次
				$class_num_all =0;
				$result_contract = $ContractModel->where(['student_id' => $item['id']])->select();
				foreach ($result_contract as $key => $value){
					$course_num_arr = explode(',',$value['course_nums']);
					for($i = 0; $i < count($course_num_arr); $i ++){
						$class_num_all= $class_num_all + $course_num_arr[$i];
					}
				}
				$item['class_ratio'] = '<span style="color:#FF5722;font-weight: bold;">'.$class_num.'</span>'.'/'.'<span style="color:#01AAED;font-weight: bold;">'.$class_num_all.'</span>';



				//1V1总课消
				$where_elimination_1v1['student_id'] = $item['id'];
				$res_total_1v1_amount = $StudentSubjectInfosClassModel->where($where_elimination_1v1)->sum('amount_deducted');  //费用
				$res_total_1v1_amount = round(abs($res_total_1v1_amount / 100),2);

				$res_total_1v1_nums = $StudentSubjectInfosClassModel->where($where_elimination_1v1)->count();  //次数


				$res_total_1v1_duration = $StudentSubjectInfosClassModel->where($where_elimination_1v1)->sum('duration');  //时长
				$res_total_1v1_duration = $res_total_1v1_duration / 60; //小时

				$item['class_elimination_one'] = $res_total_1v1_amount.'(元)/'.$res_total_1v1_nums.'(人次)'.'/'.$res_total_1v1_duration.'(小时)';



				//1V1欠费课消
				$where_elimination_1v1_arrears['student_id'] = $item['id'];
				$res_arrears_total_1v1_amount = $StudentSubjectInfosClassModel->where($where_elimination_1v1_arrears)->sum('arrears_amount');  //费用
				$res_arrears_total_1v1_amount = round(abs($res_arrears_total_1v1_amount / 100),2);

				$res_arrears_total_1v1_nums = $StudentSubjectInfosClassModel->where($where_elimination_1v1_arrears)->count();  //次数


				$res_arrears_total_1v1_duration = $StudentSubjectInfosClassModel->where($where_elimination_1v1_arrears)->sum('duration');  //时长
				$res_arrears_total_1v1_duration = $res_arrears_total_1v1_duration / 60; //小时

				$item['class_elimination_one_arrears'] = $res_arrears_total_1v1_amount.'(元)/'.$res_arrears_total_1v1_nums.'(人次)'.'/'.$res_arrears_total_1v1_duration.'(小时)';




				//1VN总课消
				$where_elimination_1vn['student_id'] = $item['id'];
				$res_total_1vn_amount = $StudentSubjectInfosClassModel->where($where_elimination_1vn)->sum('amount_deducted');  //费用
				$res_total_1vn_amount = round(abs($res_total_1vn_amount / 100),2);

				$res_total_1vn_nums = $StudentSubjectInfosClassModel->where($where_elimination_1vn)->count();  //次数


				$res_total_1vn_duration = $StudentSubjectInfosClassModel->where($where_elimination_1vn)->sum('duration');  //时长
				$res_total_1vn_duration = $res_total_1vn_duration / 60; //小时

				$item['class_elimination_n'] = $res_total_1vn_amount.'(元)/'.$res_total_1vn_nums.'(人次)'.'/'.$res_total_1vn_duration.'(小时)';



				//1VN欠费课消
				$where_elimination_1vn_arrears['student_id'] = $item['id'];
				$res_arrears_total_1vn_amount = $StudentSubjectInfosClassModel->where($where_elimination_1vn_arrears)->sum('arrears_amount');  //费用
				$res_arrears_total_1vn_amount = round(abs($res_arrears_total_1vn_amount / 100),2);

				$res_arrears_total_1vn_nums = $StudentSubjectInfosClassModel->where($where_elimination_1vn_arrears)->count();  //次数


				$res_arrears_total_1vn_duration = $StudentSubjectInfosClassModel->where($where_elimination_1vn_arrears)->sum('duration');  //时长
				$res_arrears_total_1vn_duration = $res_arrears_total_1vn_duration / 60; //小时

				$item['class_elimination_n_arrears'] = $res_arrears_total_1vn_amount.'(元)/'.$res_arrears_total_1vn_nums.'(人次)'.'/'.$res_arrears_total_1vn_duration.'(小时)';



				//班课总课消
				$where_elimination_class['student_id'] = $item['id'];
				$res_total_class_amount = $StudentSubjectInfosClassModel->where($where_elimination_class)->sum('amount_deducted');  //费用
				$res_total_class_amount = round(abs($res_total_class_amount / 100),2);

				$res_total_class_nums = $StudentSubjectInfosClassModel->where($where_elimination_class)->count();  //次数


				$res_total_class_duration = $StudentSubjectInfosClassModel->where($where_elimination_class)->sum('duration');  //时长
				$res_total_class_duration = $res_total_class_duration / 60; //小时

				$item['class_elimination'] = $res_total_class_amount.'(元)/'.$res_total_class_nums.'(人次)'.'/'.$res_total_class_duration.'(小时)';



				//班课欠费课消
				$where_elimination_class_arrears['student_id'] = $item['id'];
				$res_arrears_total_class_amount = $StudentSubjectInfosClassModel->where($where_elimination_class_arrears)->sum('arrears_amount');  //费用
				$res_arrears_total_class_amount = round(abs($res_arrears_total_class_amount / 100),2);

				$res_arrears_total_class_nums = $StudentSubjectInfosClassModel->where($where_elimination_class_arrears)->count();  //次数


				$res_arrears_total_class_duration = $StudentSubjectInfosClassModel->where($where_elimination_class_arrears)->sum('duration');  //时长
				$res_arrears_total_class_duration = $res_arrears_total_class_duration / 60; //小时

				$item['class_elimination_arrears'] = $res_arrears_total_class_amount.'(元)/'.$res_arrears_total_class_nums.'(人次)'.'/'.$res_arrears_total_class_duration.'(小时)';



				//总课消
				$class_elimination_total = $res_total_1v1_amount + $res_total_1vn_amount + $res_total_class_amount;
				$class_elimination_nums_total = $res_total_1v1_nums + $res_total_1vn_nums + $res_total_class_nums;
				$class_elimination_duration_total = $res_total_1v1_duration + $res_total_1vn_duration + $res_total_class_duration;
				$item['class_elimination_total'] = $class_elimination_total.'(元)/'.$class_elimination_nums_total.'(人次)'.'/'.$class_elimination_duration_total.'(小时)';

				//总欠费课消
				$class_elimination_total_arrears = $res_arrears_total_1v1_amount + $res_arrears_total_1vn_amount + $res_arrears_total_class_amount;
				$class_elimination_nums_total_arrears = $res_arrears_total_1v1_nums + $res_arrears_total_1vn_nums + $res_arrears_total_class_nums;
				$class_elimination_duration_total_arrears = $res_arrears_total_1v1_duration + $res_arrears_total_1vn_duration + $res_arrears_total_class_duration;
				$item['class_elimination_total_arrears'] = $class_elimination_total_arrears.'(元)/'.$class_elimination_nums_total_arrears.'(人次)'.'/'.$class_elimination_duration_total_arrears.'(小时)';
			}
		}
		return $listData;
	}

	/**
	 * 统计库存
	 * User: Feng
	 * Date: 2020/09/15
	 *
	 * @param $param
	 * @return array
	 * @throws \think\Exception
	 * @throws \think\exception\DbException
	 */
	public function getStockList($param)
	{
		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;


		$where = [];
		$where['status'] = 1;
		$where['wtype'] = 5;
		$userModel = new UserModel();
		$listData = $userModel::where($where)
			->paginate($listRows, false, ['page' => $page])
			->toArray();
		$class_0 = 0;
		$class_1 = 0;
		$class_2 = 0;
		$class_3 = 0;
		$class_4 = 0;
		$class_5 = 0;
		$total = 0;

		$where_student = [];

		if (isset($param['day']) && '' !== $param['day']) {
			$date = explode(' - ', $param['day']);
			$start = $date[0];
			$end = $date[1];
			$where_student['follow_up_date'] = [['egt', $start], ['elt', $end]];
		}
		$where_student['status'] = 1;
		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {
				//今日跟进，昨天跟进，本周跟进，上周跟进，本月跟进，上月跟进，今年跟进，去年跟进
				$where_student['user_id'] = $item['id'];

				$item['class_0'] = $this->where($where_student)->where(['intentional_grade'=>0])->count();
				$class_0 = $class_0 + $item['class_0'];

				$item['class_1'] = $this->where($where_student)->where(['intentional_grade'=>1])->count();
				$class_1 = $class_1 + $item['class_1'];

				$item['class_2'] = $this->where($where_student)->where(['intentional_grade'=>2])->count();
				$class_2 = $class_2 + $item['class_2'];

				$item['class_3'] = $this->where($where_student)->where(['intentional_grade'=>3])->count();
				$class_3 = $class_3 + $item['class_3'];

				$item['class_4'] = $this->where($where_student)->where(['intentional_grade'=>4])->count();
				$class_4 = $class_4 + $item['class_4'];

				$item['class_5'] = $this->where($where_student)->where(['intentional_grade'=>5])->count();
				$class_5 = $class_5 + $item['class_5'];

				$item['total'] = $class_0 + $class_1 + $class_2 + $class_3 + $class_4 + $class_5;
				$total = $total + $item['total'];
			}
		}

		$item_end['name'] =  '合计';
		$item_end['total'] =  $total;
		$item_end['class_0'] = $class_0;
		$item_end['class_1'] = $class_1;
		$item_end['class_2'] = $class_2;
		$item_end['class_3'] = $class_3;
		$item_end['class_4'] = $class_4;
		$item_end['class_5'] = $class_5;
		$listData['data'][count($listData['data'])] = $item_end;

		return $listData;

	}
}
